<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Controlling Cacheability of Output</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
<link rel="home" href="index.html" title="Smarty 3 Manual">
<link rel="up" href="caching.html" title="Chapter 15. Caching">
<link rel="prev" href="caching.groups.html" title="Cache Groups">
<link rel="next" href="caching.custom.html" title="Custom Cache Implementation">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">Controlling Cacheability of Output</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="caching.groups.html">Prev</a> </td>
<th width="60%" align="center">Chapter 15. Caching</th>
<td width="20%" align="right"> <a accesskey="n" href="caching.custom.html">Next</a>
</td>
</tr>
</table>
<hr>
</div>
<div class="sect1" title="Controlling Cacheability of Output">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="caching.cacheable"></a>Controlling Cacheability of Output</h2></div></div></div>
<p>
   If caching is enabled normally the whole final output of the page gets cached. However Smarty3 offers
   several options how to exclude sections of your output from caching.
  </p>
<div class="note" title="General Note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">General Note</h3>
<p>
    Be sure any variables used within a non-cached section are 
    also assigned from PHP when the page is loaded from the cache.
   </p>
</div>
<div class="sect2" title="Cacheability of Template Section">
<div class="titlepage"><div><div><h3 class="title">
<a name="cacheability.sections"></a>Cacheability of Template Section</h3></div></div></div>
<p>
    A larger section of your template can easily excluded from caching by using the <a class="link" href="language.function.nocache.html" title="{nocache}"><code class="varname">{nocache}</code></a>
    and <a class="link" href="language.function.nocache.html" title="{nocache}"><code class="varname">{/nocache}</code></a> tags.
   </p>
<div class="example">
<a name="id451818"></a><p class="title"><b>Example 15.10. Preventing a template section from being cached</b></p>
<div class="example-contents">
<pre class="programlisting">


Today's date is
{nocache}
{$smarty.now|date_format}
{/nocache}

   </pre>
<p>
     The above code will output the current date on a cached page.
    </p>
</div>
</div>
<br class="example-break">
</div>
<div class="sect2" title="Cacheability of Tags">
<div class="titlepage"><div><div><h3 class="title">
<a name="cacheability.tags"></a>Cacheability of Tags</h3></div></div></div>
<p>
    Caching for an individual tag can be disabled by adding the "nocache" option flag to the tag.
   </p>
<div class="example">
<a name="id452292"></a><p class="title"><b>Example 15.11. Preventing tag output from being cached</b></p>
<div class="example-contents"><pre class="programlisting">

Today's date is
{$smarty.now|date_format nocache}

    </pre></div>
</div>
<br class="example-break">
</div>
<div class="sect2" title="Cacheability of Variables">
<div class="titlepage"><div><div><h3 class="title">
<a name="cacheability.variables"></a>Cacheability of Variables</h3></div></div></div>
<p>
    You can <a class="link" href="api.assign.html" title="assign()"><code class="varname">assign()</code></a> variables as not cachable.
    Any tag which uses such variable will be automatically executed in nocache mode.
   </p>
<div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p>
     If a tag is executed in nocache mode you must make sure that all other variables used by that tag are 
     also assigned from PHP when the page is loaded from the cache.
    </p>
</div>
<div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p>
     The nocache status of an assigned variable will effect the compiled template code. If you change the status you must manually 
     delete existing compiled and cached template files to force a recompile.
    </p>
</div>
<div class="example">
<a name="id452350"></a><p class="title"><b>Example 15.12. Nocache Variables</b></p>
<div class="example-contents"><pre class="programlisting">

// assign $foo as nocahe variable
$smarty-&gt;assign('foo',time(),true);


Dynamic time value is {$foo}

    </pre></div>
</div>
<br class="example-break">
</div>
<div class="sect2" title="Cacheability of Plugins">
<div class="titlepage"><div><div><h3 class="title">
<a name="cacheability.plugins"></a>Cacheability of Plugins</h3></div></div></div>
<p>
      The cacheability of plugins can be
   declared when registering them. The third parameter to
   <a class="link" href="api.register.plugin.html" title="registerPlugin()"><code class="varname">registerPlugin()</code></a>
    is called <em class="parameter"><code>$cacheable</code></em> and defaults to <code class="constant">TRUE</code>.
  </p>
<p>
   When registering a plugin with <code class="literal">$cacheable=false</code> the plugin
   is called everytime the page is displayed, even if the page comes
   from the cache. The plugin function behaves a little like an
   <a class="link" href="plugins.inserts.html" title="Inserts"><code class="varname">{insert}</code></a> function.
  </p>
<div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p>
     The <em class="parameter"><code>$cacheable</code></em> status will effect the compiled template code. If you change the status you must manually 
     delete existing compiled and cached template files to force a recompile.
    </p>
</div>
<p>
   In contrast to <a class="link" href="plugins.inserts.html" title="Inserts"><code class="varname">{insert}</code>
   </a>
   the attributes to the plugins are not cached by default. They can be
   declared to be cached with the fourth parameter
   <em class="parameter"><code>$cache_attrs</code></em>. <em class="parameter"><code>$cache_attrs</code></em>
   is an array of attribute-names that should be cached, so the
   plugin-function get value as it was the time the page was written
   to cache everytime it is fetched from the cache.
  </p>
<div class="example">
<a name="id452460"></a><p class="title"><b>Example 15.13. Preventing a plugin's output from being cached</b></p>
<div class="example-contents">
<pre class="programlisting">

&lt;?php
$smarty-&gt;setCaching(Smarty::CACHING_LIFETIME_CURRENT);

function remaining_seconds($params, $smarty) {
    $remain = $params['endtime'] - time();
    if($remain &gt;= 0){
        return $remain . ' second(s)';
    }else{
        return 'done';
    }
}

$smarty-&gt;registerPlugin('function','remaining', 'remaining_seconds', false, array('endtime'));

if (!$smarty-&gt;isCached('index.tpl')) {
    // fetch $obj from db and assign...
    $smarty-&gt;assignByRef('obj', $obj);
}

$smarty-&gt;display('index.tpl');
?&gt;

   </pre>
<p>
    where <code class="filename">index.tpl</code> is:
   </p>
<pre class="programlisting">

Time Remaining: {remaining endtime=$obj-&gt;endtime}

   </pre>
<p>
    The number of seconds till the endtime of <code class="literal">$obj</code> is reached
    changes on each display of the page, even if the page is cached. Since the
    endtime attribute is cached the object only has to be pulled from the
    database when page is written to the cache but not on subsequent requests
    of the page.
   </p>
</div>
</div>
<br class="example-break"><div class="example">
<a name="id452510"></a><p class="title"><b>Example 15.14. Preventing a whole passage of a template from being cached</b></p>
<div class="example-contents">
<pre class="programlisting">

index.php:

&lt;?php
$smarty-&gt;setCaching(Smarty::CACHING_LIFETIME_CURRENT);

function smarty_block_dynamic($param, $content, $smarty) {
    return $content;
}
$smarty-&gt;registerPlugin('block','dynamic', 'smarty_block_dynamic', false);

$smarty-&gt;display('index.tpl');
?&gt;

   </pre>
<p>
    where <code class="filename">index.tpl</code> is:
   </p>
<pre class="programlisting">

Page created: {'0'|date_format:'%D %H:%M:%S'}

{dynamic}

Now is: {'0'|date_format:'%D %H:%M:%S'}

... do other stuff ...

{/dynamic}

   </pre>
</div>
</div>
<br class="example-break"><p>
   When reloading the page you will notice that both dates differ. One
   is <span class="quote">“<span class="quote">dynamic</span>”</span> one is <span class="quote">“<span class="quote">static</span>”</span>. You can do everything
    between <code class="literal">{dynamic}...{/dynamic}</code> and be sure it will not
    be cached like the rest of the page.
  </p>
<div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p>
    The above example shall just demonstrate how a dynamic block plugins works.
    See <a class="link" href="caching.cacheable.html#cacheability.sections" title="Cacheability of Template Section"><em class="parameter"><code>Cacheability of Template Section</code></em></a> on how to disable 
    caching of a template section by the built-in <a class="link" href="language.function.nocache.html" title="{nocache}"><code class="varname">{nocache}</code></a>
    and <a class="link" href="language.function.nocache.html" title="{nocache}"><code class="varname">{/nocache}</code></a> tags.
   </p>
</div>
</div>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="caching.groups.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="caching.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="caching.custom.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Cache Groups </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> Custom Cache Implementation</td>
</tr>
</table>
</div>
</body>
</html>
